package it.uniba.di.mas.jpnde.agent;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import it.uniba.di.mas.jpnde.behaviour.JPNDEConSeqBehaviour;
import it.uniba.di.mas.jpnde.behaviour.PNManagerDelivery;
import it.uniba.di.mas.jpnde.console.ConsoleFactory;
import it.uniba.di.mas.jpnde.console.IConsoleBackEnd;
import it.uniba.di.mas.jpnde.console.IConsoleUI;
import it.uniba.di.mas.jpnde.core.Arc;
import it.uniba.di.mas.jpnde.core.PN;
import it.uniba.di.mas.jpnde.core.PNObject;
import it.uniba.di.mas.jpnde.core.Place;
import it.uniba.di.mas.jpnde.core.Script;
import it.uniba.di.mas.jpnde.core.ScriptCondition;
import it.uniba.di.mas.jpnde.core.ScriptExpression;
import it.uniba.di.mas.jpnde.core.ScriptFunction;
import it.uniba.di.mas.jpnde.core.ScriptImport;
import it.uniba.di.mas.jpnde.core.Transition;
import it.uniba.di.mas.jpnde.core.PlaceEvent;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.ContainerID;
import jade.core.behaviours.Behaviour;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.CreateAgent;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.proto.AchieveREInitiator;
import jade.lang.acl.ACLMessage;
import jade.util.leap.ArrayList;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.Map;

public class JPNDEConsole extends Agent
	implements IConsoleBackEnd{
	
	private IConsoleUI console=null;
	private List managedPNs=null;
	private Behaviour currentBehaviour=null;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void setup() {
		System.out.println("JPNDE Console:"+getAID().getName()+" is ready.");
		
		managedPNs=new ArrayList();
		
		getContentManager().registerLanguage(new SLCodec(), FIPANames.ContentLanguage.FIPA_SL);
		getContentManager().registerOntology(JADEManagementOntology.getInstance());
		
		console=ConsoleFactory.createConsole(this);
		
		// Creo l'agente AMS Subscriber
		try	{
			CreateAgent ca=new CreateAgent();
			ca.setAgentName(getAID().getName()+"Ams");
			ca.setClassName("it.uniba.di.mas.jpnde.agent.JPNDEAmsSubscriber");
			ca.setContainer(new ContainerID(getContainerController().getContainerName(),null));
			
			Action act=new Action(getAMS(),ca);
			
			ACLMessage request=new ACLMessage(ACLMessage.REQUEST);
			request.addReceiver(getAMS());
			request.setOntology(JADEManagementOntology.getInstance().getName());
			request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
			request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
	
			getContentManager().fillContent(request, act);
			
			addBehaviour(new AchieveREInitiator(this,request){
				protected void handleInform(ACLMessage inform){
					console.appendLogMessage("AMS Subscriber creato !");
				}
				protected void handleFailure(ACLMessage inform){
					console.appendLogMessage("Errore nella creazione dell'AMS Subscriber !");
				}
			});
			}
		catch(Exception e){
			e.printStackTrace();
			console.appendLogMessage("Errore nella creazione dell'AMS Subscriber ! <"+e.getMessage()+">");
			}
	}

	@Override
	public synchronized void startAll()
		throws Exception
	{
		// TODO Auto-generated method stub
		
		JPNDEConSeqBehaviour seqBehaviour=new JPNDEConSeqBehaviour (this,console);
		for(int i=0;i<managedPNs.size();i++)
		{
			PN pn=(PN)managedPNs.get(i);
			pn.setConsoleId(getName());
			pn.setAgentId("");
			pn.setState(PN.STATE_STARTING);
			
			CreateAgent ca=new CreateAgent();
			ca.setAgentName(pn.getId());
			ca.setClassName("it.uniba.di.mas.jpnde.agent.PNManager");
			
			if (!pn.getContainer().equals(""))
				ca.setContainer(new ContainerID(pn.getContainer(),null));
			else
				ca.setContainer(new ContainerID(getContainerController().getContainerName(),null));
			
			Action act=new Action(getAMS(),ca);
			ACLMessage request=new ACLMessage(ACLMessage.REQUEST);
			request.addReceiver(getAMS());
			request.setOntology(JADEManagementOntology.getInstance().getName());
			request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
			request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
		
			getContentManager().fillContent(request, act);
			
			seqBehaviour.addSubBehaviour(new PNManagerDelivery(seqBehaviour,this,
					request,pn,pn.getId()));
		}
		console.managedPNsChange();
		if (currentBehaviour!=null&&
				currentBehaviour.getExecutionState().equals(Behaviour.STATE_BLOCKED))
				currentBehaviour.restart();
		addBehaviour(seqBehaviour);	
	}
	
	@Override
	public List getManagedPNs() {
		// TODO Auto-generated method stub
		return this.managedPNs;
	}

	@Override
	public synchronized void add(String pathFile)
		throws Exception
		{
		// TODO Auto-generated method stub
		console.appendLogMessage("Loading PNML:"+pathFile);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(pathFile);
		Node node = doc.getDocumentElement();
		if (!node.getNodeName().equalsIgnoreCase(PN.XMLFILE_NODE))
			throw new Exception("Node <"+PN.XMLNODE+"> not found !");
		
		
		// Reti
		NodeList rootList=node.getChildNodes();	
		for(int i=0;i<rootList.getLength();i++)
			{
			Node net=rootList.item(i);
			if ((net.getNodeName().equalsIgnoreCase(PN.XMLNODE)&&
				net.getNodeType()==Node.ELEMENT_NODE)){
				PN pn=new PN();
				pn.setId(getAttribute(net,"id"));
				pn.setNetId(pn.getId());
				
				ArrayList vPlaces=new ArrayList();
				ArrayList vTransitions=new ArrayList();
				ArrayList vArcs=new ArrayList();
					
				NodeList netChilds=net.getChildNodes();	
				for(int j=0;j<netChilds.getLength();j++)
					{
					Node netChild=netChilds.item(j);
					
					if (netChild.getNodeName().equalsIgnoreCase(Place.XMLNODE))
						vPlaces.add(netChild);
					
					if (netChild.getNodeName().equalsIgnoreCase(Transition.XMLNODE))
						vTransitions.add(netChild);
					
					if (netChild.getNodeName().equalsIgnoreCase(Arc.XMLNODE))
						vArcs.add(netChild);
					
					if (netChild.getNodeName().equalsIgnoreCase(PN.XMLPAGE_NODE))
						parsePage(netChild,vPlaces,vTransitions,vArcs);
					
					if (netChild.getNodeName().equalsIgnoreCase(PN.XMLNAME_NODE))
						pn.setName(parseLabel(netChild));
					
					if (netChild.getNodeName().equalsIgnoreCase(PN.XMLTOOL_SPEC_NODE)&&
						getAttribute(netChild,"tool").equalsIgnoreCase("JPNDE"))
						{
							NodeList specChilds=netChild.getChildNodes();
							for(int k=0;k<specChilds.getLength();k++){
								Node specChild=specChilds.item(k);
								if (specChild.getNodeName().equalsIgnoreCase(PN.XMLCONTAINER_NODE))
									pn.setContainer(getAttribute(specChild,"id",true));
							}
								
						}
					}
				
				pn.setPlaces(parsePlaces(vPlaces));
				pn.setTransitions(parseTransitions(vTransitions));
				parseArcs(pn,vArcs);
				
				System.out.println(">>> PN:"+pn.toString());
				
				pn.setState(PN.STATE_READY);
				managedPNs.add(pn);
				}
			}
		console.managedPNsChange();
	}
	
	private String getAttribute(Node node,String id)
		throws Exception
	{
		return getAttribute(node,id,false);
	}
	
	private String getAttribute(Node node,String id,boolean opt)
		throws Exception
	{
		String ret=null;
		NamedNodeMap attrs=node.getAttributes();
		for(int i=0;i<attrs.getLength();i++)
			{
			Node attr=attrs.item(i);
			if (attr.getNodeName().equalsIgnoreCase(id))
				{
				ret=attr.getNodeValue();
				break;
				}
			}
		
		if (ret==null&&!opt)
				throw new Exception("Attribute <"+id+"> not found in node <"+node.getNodeName()+"> !");
		else
			if (ret==null)
				ret="";
		return ret;
	}
	
	public String parseLabel(Node node)
		throws Exception
	{
		String ret="";
		NodeList labelChilds=node.getChildNodes();	
		for(int i=0;i<labelChilds.getLength();i++)
			{
			Node labelChild=labelChilds.item(i);
			if (labelChild.getNodeName().equalsIgnoreCase("text"))
				ret=labelChild.getTextContent();
			}
		return ret;
	}		
	
	public void parsePage(Node node, 
			ArrayList vPlaces, ArrayList vTransitions, ArrayList vArcs)
		throws Exception
	{
		NodeList pageChilds=node.getChildNodes();	
		for(int i=0;i<pageChilds.getLength();i++)
		{
		Node pageChild=pageChilds.item(i);
		if (pageChild.getNodeName().equalsIgnoreCase(Place.XMLNODE))
			vPlaces.add(pageChild);
		if (pageChild.getNodeName().equalsIgnoreCase(Transition.XMLNODE))
			vTransitions.add(pageChild);
		if (pageChild.getNodeName().equalsIgnoreCase(Arc.XMLNODE))
			vArcs.add(pageChild);
		if (pageChild.getNodeName().equalsIgnoreCase(PN.XMLPAGE_NODE))
			parsePage(node,vPlaces,vTransitions,vArcs);
		}
	
	}
	
	private ArrayList parsePlaces(ArrayList placeNodes)
		throws Exception
	{
		ArrayList ret=new ArrayList();
		for(int i=0;i<placeNodes.size();i++){
			Place place=new Place();
			Node placeNode=(Node)placeNodes.get(i);
			place.setId(getAttribute(placeNode,"id"));
			
			NodeList placeChilds=placeNode.getChildNodes();	
			for(int j=0;j<placeChilds.getLength();j++){
				Node placeChild=placeChilds.item(j);
				
				if (placeChild.getNodeName().equalsIgnoreCase(Place.XMLNAME_NODE))
					place.setName(parseLabel(placeChild));
				
				if (placeChild.getNodeName().equalsIgnoreCase(Place.XMLINITMARK_NODE))
					{
					String sIniMark=parseLabel(placeChild);
					try{
						place.setInitialMark(Long.parseLong(sIniMark));
						}
					catch (NumberFormatException ee){
						throw new Exception("Parsing initialMarking of place <"+place.getId()+">");
						}
					}
				
				if (placeChild.getNodeName().equalsIgnoreCase(PN.XMLTOOL_SPEC_NODE)&&
					getAttribute(placeChild,"tool").equalsIgnoreCase("JPNDE")){
					
					NodeList specChilds=placeChild.getChildNodes();
					for(int k=0;k<specChilds.getLength();k++){
						Node specChild=specChilds.item(k);
						// event In
						if (specChild.getNodeName().equalsIgnoreCase(Place.XMLEVENTIN_NODE))
							{
							PlaceEvent event=new PlaceEvent();
							event.setId(place.getId());
							event.setType(PlaceEvent.TYPE_INPUT);
							event.setAuto(getAttribute(specChild,"auto",true));
							try{
								String sDelay=getAttribute(specChild,"delay",true);
								event.setDelay(Long.parseLong(sDelay.equals("")?"0":sDelay));
							}
							catch (NumberFormatException ee){
								throw new Exception("Parsing delay of place <"+place.getId()+">");
								}
							place.setEvent(event);
							}
						
						// event Out
						if (specChild.getNodeName().equalsIgnoreCase(Place.XMLEVENTOUT_NODE))
							{
							PlaceEvent event=new PlaceEvent();
							event.setId(place.getId());
							event.setType(PlaceEvent.TYPE_OUTPUT);
							event.setRoute(getAttribute(specChild,"route",true));
							place.setEvent(event);
							}
						
						// endPlace
						if (specChild.getNodeName().equalsIgnoreCase(Place.XMLENDPLACE_NODE))
							{
							try{
								place.setEndPlace("Y");
								String sThreshold=getAttribute(specChild,"threshold",true);
								place.setEndThreshold(Long.parseLong(sThreshold.equals("")?"1":sThreshold));
								}
							catch (NumberFormatException ee){
								throw new Exception("Parsing endThreshold of place <"+place.getId()+">");
								}
							}
						// script
						if (specChild.getNodeName().equalsIgnoreCase(Place.XMLJPNSCRIPT_NODE))
								parseScript(place,specChild);
					}
				} // end if tool spec
			}
			ret.add(place);
		}
		return ret;
	}

	private void parseScript(PNObject pnObject,Node scriptNode)
		throws Exception
	{
		Script script=new Script();
		NodeList nodeChilds=scriptNode.getChildNodes();
		for(int i=0;i<nodeChilds.getLength();i++){
			Node nodeChild=nodeChilds.item(i);
			// import
			if (nodeChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNIMPORT_NODE)){
				ScriptImport sImport=new ScriptImport(getAttribute(nodeChild,"package"));
				script.addImport(sImport);
			}
			// function
			if (nodeChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNFUNCTION_NODE)){
				ScriptFunction sFunction=new ScriptFunction();
				sFunction.setOnEvent(getAttribute(nodeChild,"onEvent"));
				
				NodeList functionChilds=nodeChild.getChildNodes();
				
				boolean bBody=false;
				for(int k=0;k<functionChilds.getLength();k++){
					Node body=functionChilds.item(k);
					if (body.getNodeName().
							equalsIgnoreCase(PNObject.XMLJPNFUNCTION_BODY_NODE)){
							bBody=true;
							sFunction.setBody(body.getTextContent());
						}
				}
				
				if (!bBody)
					throw new Exception("<body> tag not found for function <"+
							sFunction.toString()+"> on <"+pnObject.getId()+"> !");
				script.addFunction(sFunction);
			}
				
		}
		pnObject.setScript(script);
	}
	
	private ArrayList parseTransitions(ArrayList transNodes)
		throws Exception
	{
	ArrayList ret=new ArrayList();
	for(int i=0;i<transNodes.size();i++){
		Transition transition=new Transition();
		Node transNode=(Node)transNodes.get(i);
		transition.setId(getAttribute(transNode,"id"));
		
		NodeList transChilds=transNode.getChildNodes();	
		for(int j=0;j<transChilds.getLength();j++){
			Node transChild=transChilds.item(j);
			
			if (transChild.getNodeName().equalsIgnoreCase(Transition.XMLNAME_NODE))
				transition.setName(parseLabel(transChild));
			
			if (transChild.getNodeName().equalsIgnoreCase(Transition.XMLTOOL_SPEC_NODE)&&
					getAttribute(transChild,"tool").equalsIgnoreCase("JPNDE")){
				
				NodeList specChilds=transChild.getChildNodes();
				for(int k=0;k<specChilds.getLength();k++){
					Node specChild=specChilds.item(k);
					
					if (specChild.getNodeName().equalsIgnoreCase(Transition.XMLCONTAINER_NODE))
						transition.setContainer(getAttribute(specChild,"id",true));
					
					if (specChild.getNodeName().equalsIgnoreCase(Transition.XMLIMPLCLASS_NODE))
						transition.setImplClass(getAttribute(specChild,"name",true));
					
					// Script
					if (specChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNSCRIPT_NODE))
						parseScript(transition,specChild);
				
					}
				} // spec node
			}
		ret.add(transition);
		}
	return ret;
	}

	private void parseArcs(PN pn, ArrayList arcNodes)
		throws Exception
	{
		Map placesMap=new HashMap();
		for(int i=0;i<pn.getPlaces().size();i++){
			Place place=(Place)pn.getPlaces().get(i);
			placesMap.put(place.getId(),place);
		}
		
		Map transMap=new HashMap();
		for(int i=0;i<pn.getTransitions().size();i++){
			Transition transition=(Transition)pn.getTransitions().get(i);
			transMap.put(transition.getId(),transition);
		}
		
		for(int i=0;i<arcNodes.size();i++){
			Arc arc=new Arc();
			Node arcNode=(Node)arcNodes.get(i);
			arc.setId(getAttribute(arcNode,"id"));
			NodeList arcChilds=arcNode.getChildNodes();
			
			String source=getAttribute(arcNode,"source");
			String target=getAttribute(arcNode,"target");
			
			boolean preset=true;
			Transition transition=(Transition)transMap.get(source);
			if (transition!=null)
				preset=false;
			else
				transition=(Transition)transMap.get(target);
			if (transition==null)
				throw new Exception("Transition not found for Arc<"+arc.getId()+">");
			
			Place place=null;
			if (preset)
				place=(Place)placesMap.get(source);
			else
				place=(Place)placesMap.get(target);
			if (place==null)
				throw new Exception("Place not found for Arc<"+arc.getId()+">");
			arc.setPlaceId(place.getId());	
			
			for(int j=0;j<arcChilds.getLength();j++){
				Node arcChild=arcChilds.item(j);
				
				if (arcChild.getNodeName().equalsIgnoreCase(Arc.XMLINSCRIPTION_NODE)){
					String sIncr=parseLabel(arcChild);
					try{
						arc.setWeight(Integer.parseInt(sIncr));
						}
					catch (NumberFormatException ee){
						throw new Exception("Parsing inscription of arc <"+arc.getId()+">");
						}
				}
				
				if (arcChild.getNodeName().equalsIgnoreCase(Transition.XMLTOOL_SPEC_NODE)&&
						getAttribute(arcChild,"tool").equalsIgnoreCase("JPNDE")){
					
					NodeList specChilds=arcChild.getChildNodes();
					for(int k=0;k<specChilds.getLength();k++){
						Node specChild=specChilds.item(k);
						// Condition
						if (specChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNCONDITION_NODE))
							parseCondition(arc,specChild);
						// Script
						if (specChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNSCRIPT_NODE))
							parseScript(arc,specChild);
					}
				}
				
				
			} // end for childs
			
			if (preset)
				transition.getPreset().add(arc);
			else
				transition.getPostset().add(arc);
			
		} // end for
	}

	private void parseCondition(Arc arc,Node condNode)
		throws Exception
	{
		ScriptCondition cond=new ScriptCondition();
		String sPriority=getAttribute(condNode,"priority",true);
		if ((sPriority!=null)&&(!sPriority.equals(""))){
			try{
				cond.setPriority(Integer.parseInt(sPriority));
				}
			catch (NumberFormatException ee){
				throw new Exception("Parsing priority of condition <"+arc.getId()+">");
				}
		}
		NodeList nodeChilds=condNode.getChildNodes();
		for(int i=0;i<nodeChilds.getLength();i++){
			Node nodeChild=nodeChilds.item(i);
			// import
			if (nodeChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNIMPORT_NODE)){
				ScriptImport sImport=new ScriptImport(getAttribute(nodeChild,"package"));
				cond.addImport(sImport);
			}
			// expression
			if (nodeChild.getNodeName().equalsIgnoreCase(PNObject.XMLJPNEXPRESSION_NODE)){
				ScriptExpression sExpr=new ScriptExpression();
				String sNumToken=getAttribute(condNode,"numToken",true);
				
				if ((sNumToken!=null)&&(!sNumToken.equals(""))){
					try{
						sExpr.setNumToken(Integer.parseInt(sNumToken));
						}
					catch (NumberFormatException ee){
						throw new Exception("Parsing numToken of expression on <"+arc.getId()+">");
						}
				}
				
				NodeList exprChilds=nodeChild.getChildNodes();
				boolean bBody=false;
				for(int k=0;k<exprChilds.getLength();k++){
					Node body=exprChilds.item(k);
					if (body.getNodeName().
							equalsIgnoreCase(PNObject.XMLJPNFUNCTION_BODY_NODE)){
							bBody=true;
							sExpr.setBody(body.getTextContent());
						}
				}
				
				if (!bBody)
					throw new Exception("<body> tag not found for expression <"+
							sExpr.toString()+"> on <"+arc.getId()+"> !");
				cond.addExpression(sExpr);
			}
				
		}
		arc.setCondition(cond);
	}	
	
	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return getAID().getName();
	}

	@Override
	public synchronized void stopAll() throws Exception {
		// TODO Auto-generated method stub
		for(int i=managedPNs.size()-1;i>=0;i--)
			{
			PN pn=(PN)managedPNs.get(i);
			if (pn.getState().equals(PN.STATE_RUNNING)){
				ACLMessage msg=new ACLMessage(ACLMessage.CANCEL);
				msg.addReceiver(new AID(pn.getAgentId(),AID.ISGUID));
				send(msg);
				}
			}
	}

	@Override
	public synchronized void clear() throws Exception {
		// TODO Auto-generated method stub
		for(int i=0;i<managedPNs.size();i++){
			PN pn=(PN)managedPNs.get(i);
			if (pn.getState().equals(PN.STATE_RUNNING))
				throw new Exception("Can't clear manged PN.\nSome PN is running !");
		}
		managedPNs.clear();
		console.managedPNsChange();
	}

	@Override
	public void start(String netName, String id)
		throws Exception
	{
		// TODO Auto-generated method stub
		PN pn=null;
		for(int i=0;i<managedPNs.size();i++){
			if (((PN)managedPNs.get(i)).getNetId().equals(netName)&&
			    ((PN)managedPNs.get(i)).getId().equals(id))
				pn=(PN)managedPNs.get(i);
			if (((PN)managedPNs.get(i)).getId().equals(id)&&
				((PN)managedPNs.get(i)).getState().equals(PN.STATE_RUNNING))
				throw new Exception("PN with id <"+id+"> is running !");
			}
		
		if (pn==null)
			for(int i=0;i<managedPNs.size();i++)
				if (((PN)managedPNs.get(i)).getNetId().equals(netName)){
					pn=(PN)managedPNs.get(i);
					break;
					}
		if (pn==null)
			throw new Exception("PN <"+netName+"> not found !");
		
		if (!pn.getId().equalsIgnoreCase(id))
		{
			pn=(PN)pn.clone();
			pn.setId(id);
			managedPNs.add(pn);
		}
		
		JPNDEConSeqBehaviour seqBehaviour=new JPNDEConSeqBehaviour (this,console);
		
		pn.setAgentId("");
		pn.setState(PN.STATE_STARTING);
		pn.setConsoleId(getName());
		
		CreateAgent ca=new CreateAgent();
		ca.setAgentName(pn.getId());
		ca.setClassName("it.uniba.di.mas.jpnde.agent.PNManager");
		
		if (!pn.getContainer().equals(""))
			ca.setContainer(new ContainerID(pn.getContainer(),null));
		else
			ca.setContainer(new ContainerID(getContainerController().getContainerName(),null));
		
		Action act=new Action(getAMS(),ca);
		ACLMessage request=new ACLMessage(ACLMessage.REQUEST);
		request.addReceiver(getAMS());
		request.setOntology(JADEManagementOntology.getInstance().getName());
		request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
		request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
	
		getContentManager().fillContent(request, act);
		
		seqBehaviour.addSubBehaviour(new PNManagerDelivery(seqBehaviour,this,
				request,pn,pn.getId()));
		
		console.managedPNsChange();
		if (currentBehaviour!=null&&
			currentBehaviour.getExecutionState().equals(Behaviour.STATE_BLOCKED))
			currentBehaviour.restart();
			
		addBehaviour(seqBehaviour);	
	}

	@Override
	public void stop(String id) {
		// TODO Auto-generated method stub
		for(int i=managedPNs.size()-1;i>=0;i--)
		{
		PN pn=(PN)managedPNs.get(i);
		if (pn.getState().equals(PN.STATE_RUNNING)&&
			pn.getId().equals(id)){
			ACLMessage msg=new ACLMessage(ACLMessage.CANCEL);
			msg.addReceiver(new AID(pn.getAgentId(),AID.ISGUID));
			send(msg);
			break;
			}
		}
	}

	@Override
	public Behaviour getCurrentBehaviour() {
		// TODO Auto-generated method stub
		return currentBehaviour;
	}

	@Override
	public void setCurrentBehaviour(Behaviour current) {
		// TODO Auto-generated method stub
		this.currentBehaviour=current;
	}

	
	
}
